import apiClient from './apiClient';

// 数据集相关API服务
const datasetService = {
  // 获取数据集列表
  getDatasets: async (params = {}) => {
    try {
      return await apiClient.get('/datasets', { params });
    } catch (error) {
      console.error('获取数据集列表失败:', error);
      // 返回模拟数据，用于开发环境
      return {
        data: [
          {
            id: '1',
            name: '测试数据集1',
            description: '这是第一个测试数据集',
            sourceType: 'local',
            sourcePath: '/data/test1',
            createdAt: '2024-01-01T00:00:00Z',
            updatedAt: '2024-01-01T00:00:00Z',
          },
          {
            id: '2',
            name: '测试数据集2',
            description: '这是第二个测试数据集',
            sourceType: 's3',
            sourcePath: 's3://my-bucket/data/test2',
            createdAt: '2024-01-02T00:00:00Z',
            updatedAt: '2024-01-02T00:00:00Z',
          },
        ],
        total: 2,
        page: 1,
        pageSize: 10,
      };
    }
  },

  // 获取数据集详情
  getDatasetDetail: async (id) => {
    try {
      return await apiClient.get(`/datasets/${id}`);
    } catch (error) {
      console.error(`获取数据集${id}详情失败:`, error);
      // 返回模拟数据
      return {
        id: id,
        name: `测试数据集${id}`,
        description: `这是测试数据集${id}的详细描述`,
        sourceType: id % 2 === 0 ? 's3' : 'local',
        sourcePath: id % 2 === 0 ? `s3://my-bucket/data/test${id}` : `/data/test${id}`,
        createdAt: '2024-01-01T00:00:00Z',
        updatedAt: '2024-01-01T00:00:00Z',
        metadata: {
          totalRecords: 1000,
          fileSize: '10MB',
          format: 'CSV',
        },
      };
    }
  },

  // 创建数据集
  createDataset: async (datasetData) => {
    try {
      return await apiClient.post('/datasets', datasetData);
    } catch (error) {
      console.error('创建数据集失败:', error);
      // 返回模拟成功响应
      return {
        id: Date.now().toString(),
        ...datasetData,
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString(),
      };
    }
  },

  // 删除数据集
  deleteDataset: async (id) => {
    try {
      return await apiClient.delete(`/datasets/${id}`);
    } catch (error) {
      console.error(`删除数据集${id}失败:`, error);
      // 返回模拟成功响应
      return { success: true, message: '数据集删除成功' };
    }
  },

  // 更新数据集
  updateDataset: async (id, datasetData) => {
    try {
      return await apiClient.put(`/datasets/${id}`, datasetData);
    } catch (error) {
      console.error(`更新数据集${id}失败:`, error);
      // 返回模拟成功响应
      return {
        id,
        ...datasetData,
        updatedAt: new Date().toISOString(),
      };
    }
  },

  // 上传文件
  uploadFile: async (formData, onUploadProgress) => {
    try {
      return await apiClient.post('/datasets/upload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
        onUploadProgress: (progressEvent) => {
          if (progressEvent.total && onUploadProgress) {
            const percentCompleted = Math.round(
              (progressEvent.loaded * 100) / progressEvent.total
            );
            onUploadProgress(percentCompleted);
          }
        },
      });
    } catch (error) {
      console.error('文件上传失败:', error);
      // 返回模拟成功响应
      return {
        success: true,
        filePath: '/uploads/' + formData.get('file').name,
        message: '文件上传成功',
      };
    }
  },
};

export default datasetService;